package com.zw.mizhe.view.activity;

import android.os.Build;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;
import com.zw.mizhe.App;
import com.zw.mizhe.R;
import com.zw.mizhe.common.base.BaseActivity;
import com.zw.mizhe.common.utils.AppManager;
import com.zw.mizhe.common.utils.FragNavController;
import com.zw.mizhe.view.fragment.HomeFragment;
import com.zw.mizhe.view.fragment.MyFragment;
import com.zw.mizhe.view.fragment.ShopCarFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * MVC
 * MVP
 * MVVM
 */
public class MainActivity extends BaseActivity implements OnTabSelectListener {
    List<Fragment> fragments = new ArrayList<>(5);

    FragNavController fragNavController;
    @BindView(R.id.main_bottomBar)
    BottomBar mainBottomBar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        fragments.add(HomeFragment.newInstance(1));
        fragments.add(HomeFragment.newInstance(2));
        fragments.add(HomeFragment.newInstance(3));
        fragments.add(ShopCarFragment.newInstance());
        fragments.add(MyFragment.newInstance());
        fragNavController = new FragNavController(getSupportFragmentManager(), R.id.main_container, fragments);
        mainBottomBar.setOnTabSelectListener(this);
    }


    @Override
    public void onTabSelected(@IdRes int tabId) {
        switch (tabId) {
            case R.id.bottom_tab_sale:
                toolbar.setTitle("米折");
                fragNavController.switchTab(FragNavController.TAB1);
                break;
            case R.id.bottom_tab_free_mail:
                toolbar.setTitle("9.9包邮");
                fragNavController.switchTab(FragNavController.TAB2);
                break;
            case R.id.bottom_tab_group_buy:
                toolbar.setTitle("拼团");
                fragNavController.switchTab(FragNavController.TAB3);
                break;
            case R.id.bottom_tab_shop_car:
                toolbar.setTitle("购物车");
                fragNavController.switchTab(FragNavController.TAB4);
                break;
            case R.id.bottom_tab_my:
                fragNavController.switchTab(FragNavController.TAB5);
                break;
        }
    }

    private long firstTime;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            //两次按键小于2秒时，退出应用
            //注销网络监听
            App.getInstance().unRegisterNetworkStateReceiver();
            //注销声明周期管理
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                App.getInstance().unRegisterActivityLifecycleCallbacks();
            }
            AppManager.getAppManager().AppExit(App.getInstance());
        }
    }

}
